package com.china08.yxyapi.api.home.kecheng.cloud;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudCatalog;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItem;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItemQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudCatalogRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemQuesRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;

/**
 * 
 * @annotation: 云课程--课程详情
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class KechengCloudLessonDetailController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsCloudLessonRepository yxyLessonsCloudLessonRepository;

    @Autowired
    private YxyLessonsCloudLessonItemRepository yxyLessonsCloudLessonItemRepository;

    @Autowired
    private YxyLessonsVideoRepository yxyLessonsVideoRepository;

    @Autowired
    private YxyLessonsCloudCatalogRepository yxyLessonsCloudCatalogRepository;

    @Autowired
    private YxyLessonsCloudLessonItemQuesRepository yxyLessonsCloudLessonItemQuesRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/cloud/lesson/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        YxyLessonsCloudLesson yxyLessonsCloudLesson = yxyLessonsCloudLessonRepository.findById(reqModel.lessonId);
        RespModel model = new RespModel();
        List<LessonItem> iList = new ArrayList<KechengCloudLessonDetailController.LessonItem>();
        // 查询课程对应课时
        if (yxyLessonsCloudLesson != null) {

            List<YxyLessonsCloudLessonItem> lItems = yxyLessonsCloudLessonItemRepository
                    .findByLessonIdAndStatusOrderByOrderNum(reqModel.lessonId, 2);
            int i = 1;
            Integer dur = 0;
            for (YxyLessonsCloudLessonItem yxyLessonsCloudLessonItem : lItems) {
                LessonItem item = new LessonItem();
                // 获取视频时长
                YxyLessonsVideo yxyLessonsVideo = yxyLessonsVideoRepository
                        .findById(yxyLessonsCloudLessonItem.getVideoId());
                if (yxyLessonsVideo != null) {
                    dur += yxyLessonsVideo.getDuration();
                }
                item.duration = yxyLessonsVideo == null ? 0 : yxyLessonsVideo.getDuration();
                item.lessonId = yxyLessonsCloudLessonItem.getLessonId();
                item.lessonItemId = yxyLessonsCloudLessonItem.getId();
                item.lessonItemIntro = yxyLessonsCloudLessonItem.getIntro();
                item.lessonItemTitle = yxyLessonsCloudLessonItem.getTitle();
                item.OrderNum = i;
                // 获取关联习题
                List<YxyLessonsCloudLessonItemQues> list = yxyLessonsCloudLessonItemQuesRepository
                        .findByLessonItemId(yxyLessonsCloudLessonItem.getId());
                if (list != null && list.size() > 0) {
                    item.isQues = 1;
                } else {
                    item.isQues = 0;
                }
                iList.add(item);
                i++;
            }
            model.author = yxyLessonsCloudLesson.getAuthor();
            model.avator = yxyLessonsCloudLesson.getAvator();
            model.count = yxyLessonsCloudLessonItemRepository.countByLessonIdAndStatus(yxyLessonsCloudLesson.getId(),
                    2);
            model.cover = yxyLessonsCloudLesson.getCover();
            model.duration = dur;
            model.clickPv = yxyLessonsCloudLesson.getClickPv() == null ? 0 : yxyLessonsCloudLesson.getClickPv();
            model.ep = yxyLessonsCloudLesson.getEp();
            model.lessonItemList = iList;
            model.name = yxyLessonsCloudLesson.getName();
            model.summary = yxyLessonsCloudLesson.getSummary();
            model.vv = yxyLessonsCloudLesson.getVv();
            model.epTimes = yxyLessonsCloudLesson.getEpTimes();
            model.isJing = yxyLessonsCloudLesson.getIsJingpinLesson();
            model.schoolName = yxyLessonsCloudLesson.getSchoolNick();
            // 获取目录名称
            String catalogId1 = yxyLessonsCloudLesson.getCatalogId1();
            String catalogId2 = yxyLessonsCloudLesson.getCatalogId2();
            String catalogId3 = yxyLessonsCloudLesson.getCatalogId3();
            String catalogId4 = yxyLessonsCloudLesson.getCatalogId4();
            String catalogId5 = yxyLessonsCloudLesson.getCatalogId5();
            if (StringUtils.isNotBlank(catalogId1)) {
                YxyLessonsCloudCatalog catalog = yxyLessonsCloudCatalogRepository.findById(catalogId1);
                model.catalogName1 = catalog.getName();
            } else {
                model.catalogName1 = "";
            }
            if (StringUtils.isNotBlank(catalogId2)) {
                YxyLessonsCloudCatalog catalog = yxyLessonsCloudCatalogRepository.findById(catalogId2);
                model.catalogName2 = "-" + catalog.getName();
            } else {
                model.catalogName2 = "";
            }
            if (StringUtils.isNotBlank(catalogId3)) {
                YxyLessonsCloudCatalog catalog = yxyLessonsCloudCatalogRepository.findById(catalogId3);
                model.catalogName3 = "-" + catalog.getName();
            } else {
                model.catalogName3 = "";
            }
            if (StringUtils.isNotBlank(catalogId4)) {
                YxyLessonsCloudCatalog catalog = yxyLessonsCloudCatalogRepository.findById(catalogId4);
                model.catalogName4 = "-" + catalog.getName();
            } else {
                model.catalogName4 = "";
            }
            if (StringUtils.isNotBlank(catalogId5)) {
                YxyLessonsCloudCatalog catalog = yxyLessonsCloudCatalogRepository.findById(catalogId5);
                model.catalogName5 = "-" + catalog.getName();
            } else {
                model.catalogName5 = "";
            }

        }

        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 课程ID
        public String lessonId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 封面
        public String cover;
        // 课程名称
        public String name;
        // 总课时
        public Integer count;
        // 总时长
        public Integer duration;
        // 课程评分（所有评分之和处于评分人数即综合评分）
        public Float ep;
        // 学习人数
        public Integer vv;
        // 点击次数
        public Integer clickPv;
        // 课程简介
        public String summary;
        // 贡献者
        public String author;
        // 贡献者学校
        public String schoolName;
        // 贡献者头像
        public String avator;
        // 评分次数
        public Integer epTimes;
        // 课时
        public List<LessonItem> lessonItemList;
        // 是否是精品
        public Integer isJing;
        // 一级目录名称
        public String catalogName1;
        // 二级目录名称
        public String catalogName2;
        // 三级目录名称
        public String catalogName3;
        // 四级目录名称
        public String catalogName4;
        // 五级目录名称
        public String catalogName5;
    }

    /** 课时 */
    protected static class LessonItem {

        // 课时名称
        public String lessonItemTitle;
        // 课时简介
        public String lessonItemIntro;
        // 课程id
        public String lessonId;
        // 课时Id
        public String lessonItemId;
        // 视频时长
        public Integer duration;
        // 序号
        public Integer OrderNum;
        // 是否有关联的习题 0 没有 1 有
        public Integer isQues;

    }
}
